package geometria;

import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;

/**
 * 
 * Un segmento es la linea que une dos Coordenadas
 * 
 */
public class Segmento {
	private Line2D.Double l;

	public Segmento(Coordenada c1, Coordenada c2) {
		l = new Line2D.Double(c1.getX(), c1.getY(), c2.getX(), c2.getY());
	}

	public boolean intersecaCon(Segmento otro) {
		return this.l.intersectsLine(otro.l);
	}

	public Coordenada getPuntoMedio() {
		Double xCentral = (this.getX1() + this.getX2()) / 2;
		Double yCentral = (this.getY1() + this.getY2()) / 2;
		return new Coordenada(xCentral, yCentral);
	}

	public Segmento getLineaGirada(Double angulo, Coordenada pivote) {

		AffineTransform transform = AffineTransform.getRotateInstance(angulo,
				pivote.getX(), pivote.getY());
		double[] coordinates = new double[6];
		Double xInicial, yInicial, xFinal, yFinal;
		PathIterator pth;

		Shape lineaGirada = transform.createTransformedShape(this.l);
		pth = lineaGirada.getPathIterator(null);

		pth.currentSegment(coordinates);
		xInicial = coordinates[0];
		yInicial = coordinates[1];

		pth.next();
		pth.currentSegment(coordinates);
		xFinal = coordinates[0];
		yFinal = coordinates[1];

		return new Segmento(new Coordenada(xInicial, yInicial), new Coordenada(
				xFinal, yFinal));

	}

	public double getX1() {
		return this.l.x1;
	}

	public double getX2() {
		return this.l.x2;
	}

	public double getY1() {
		return this.l.y1;
	}

	public double getY2() {
		return this.l.y2;
	}

	public String toString() {
		return "(" + this.getX1() + ", " + this.getY1() + ") - ("
				+ this.getX2() + ", " + this.getY2() + ")";
	}

}
